//
//  CATakeMoneyViewController.m
//  LNH
//
//  Created by twentylx on 2018/1/22.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CATakeMoneyViewController.h"
#import "CATakeMoneyView.h"
#import "CATakeMoneyApi.h"
#import "CABankCardViewController.h"

@interface CATakeMoneyViewController () <CABankCardViewControllerDelegate> {
    NSString *_bankId;
}

@property (nonatomic, strong) CATakeMoneyView *takeMoneyView;

@end

@implementation CATakeMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = RGBCOLOR(241, 241, 241);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
    
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    self.navigationItem.title = @"提现";
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.takeMoneyView];
}

#pragma mark layout
- (void) updateViewConstraints {
    [super updateViewConstraints];
    
    [_takeMoneyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark get & set
- (CATakeMoneyView *)takeMoneyView {
    if (!_takeMoneyView) {
        _takeMoneyView = [CATakeMoneyView new];
        UITapGestureRecognizer *gesture = [UITapGestureRecognizer new];
        [_takeMoneyView.bankView addGestureRecognizer:gesture];
        [[gesture rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            CABankCardViewController *bankCardVC = [CABankCardViewController new];
            bankCardVC.isSelected = YES;
            bankCardVC.delegate = self;
            [self.navigationController pushViewController:bankCardVC animated:YES];
        }];
        
        [_takeMoneyView.submitButton addTarget:self action:@selector(takeMoneyAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _takeMoneyView;
}


#pragma mark private method
- (void)takeMoneyAction {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    CATakeMoneyApi *takeMoneyApi = [[CATakeMoneyApi alloc] initWithBankId:_bankId Money:_takeMoneyView.moneyTextField.text];
    [takeMoneyApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CATakeMoneyApi *api = (CATakeMoneyApi *) request;
        if (api.done) {
            [SVProgressHUD lm_displaySuccessWithStatus:@"提现成功"];
        } else {
            [SVProgressHUD lm_displayErrorWithStatus:api.errorMsg];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
    }];
}

#pragma mark CABankListViewControllerDelegate
- (void)selectBankCardWithDict:(NSDictionary *)dict {
    _bankId = dict[@"id"];
    
    _takeMoneyView.titleLabel.hidden = YES;
    
    _takeMoneyView.bankIconImageView.hidden =NO;
    [_takeMoneyView.bankIconImageView sd_setImageWithURL:[NSURL URLWithString:dict[@"bank_logo"]]];
    
    _takeMoneyView.bankTitleLabel.hidden = NO;
    _takeMoneyView.bankTitleLabel.text = dict[@"bank_name"];
    
    _takeMoneyView.bankNoLabel.hidden = NO;
    _takeMoneyView.bankNoLabel.text = dict[@"card_number"];
}

@end
